#include <arch/types.h>
#include <string.h>

/*
 * strlen: returns the number of chars in a string
 *	@str: the string to be counted
 *
 * Returns: a size_t value with the number of characters
 *
 * NOTE: this is not my implementation. I found this in uClibc.
 * This seems to be fast as hell, as processors love to work on 32-bit
 */
size_t strlen(const char *str) {
	const char *CharPointer;
	const unsigned long int *dwordPointer;
	dword longword;
	const dword himagic = 0x80808080L, lomagic = 0x01010101L;

	/* Align CharPointer on a 4-byte boundary */
	for (CharPointer = str; ((unsigned long int) CharPointer & (sizeof(dword) - 1)) != 0; ++CharPointer)
		if (*CharPointer == '\0')
			return CharPointer - str;

	dwordPointer = (dword *) CharPointer;

	/* Check dword per dword */
	for (;;) {
		longword = *dwordPointer++;

		if (((longword - lomagic) & himagic) != 0) {
			const char *cp = (const char *) (dwordPointer - 1);

			if (cp[0] == 0)
				return cp - str;
			if (cp[1] == 0)
				return cp - str + 1;
			if (cp[2] == 0)
				return cp - str + 2;
			if (cp[3] == 0)
				return cp - str + 3;
		}
	}
}

